package com.sdcqjy.property;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.multidex.MultiDex;
import android.support.v4.content.LocalBroadcastManager;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.sdcqjy.mylibrary.ActivityManager;
import com.sdcqjy.mylibrary.FileOperateUtil;
import com.sdcqjy.mylibrary.LibApp;
import com.sdcqjy.mylibrary.Logger;
import com.sdcqjy.property.activity.LoginActivity;
import com.sdcqjy.property.mode.LoginMode;
import com.sdcqjy.property.utils.ImgSelectConfig;
import com.sdcqjy.property.utils.UserConfig;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

import static com.sdcqjy.property.MyConstants.ActionLogin;

/**
 * Created by 磁磁帅 on 2018/4/1.
 * 功能：
 */
public class AppLL extends Application {
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    private static AppLL appLL;
    public static Typeface typefaceDiy;

    public static AppLL getAppLL() {
        return appLL;
    }

    public void onCreate() {
        super.onCreate();

        appLL = this;
        init();
    }

    private boolean login;

    private void init() {
        LibApp.init(this);
        FileOperateUtil.init(this, "Sdcqjy");
        login = UserConfig.isLogin();

        ImgSelectConfig.init(this);
        //初始化刷新控件
        SmartRefreshLayout.setDefaultRefreshHeaderCreater((context, layout) -> {
            ClassicsHeader header = new ClassicsHeader(context);
            //BezierRadarHeader header = new BezierRadarHeader(context);
            layout.setPrimaryColorsId(R.color.activityBg, R.color.colorAccent);
            header.setSpinnerStyle(SpinnerStyle.Scale).setTimeGone(true);
            return header;
        });
        SmartRefreshLayout.setDefaultRefreshFooterCreater((context, layout) -> {
            ClassicsFooter footer = new ClassicsFooter(context);
            return footer;
        });
        if (Logger.isDebug) JPushInterface.setDebugMode(true);
        else {
            CrashReport.initCrashReport(getApplicationContext(), "266a15283a", false);
        }
        JPushInterface.init(this);
        if (login) {
            LoginMode data = LoginMode.getMode(this);
            setLoginId(data.id + "");
        }

        //更改字体
        typefaceDiy = Typeface.createFromAsset(getAssets(), "mkxxk.ttf");
    }

    public boolean isLogin() {
        return login;
    }

    public void setLogin(boolean login) {
        this.login = login;
        UserConfig.setLogin(login);
        //UserConfig.commit();
        Intent intent = new Intent(ActionLogin);
        intent.putExtra("login", login);
        LocalBroadcastManager.getInstance(this).sendBroadcastSync(intent);
        if (!login) {
            setLoginId("loginOut");
        }
    }

    public void setLoginMode(LoginMode data) {
        LoginMode.setMode(this, data);
        UserConfig.setToken(data.token);
        setLoginId(data.id + "");
    }

    /**
     * 设置极光标签
     */
    public void setLoginId(String uuid) {
        if (Logger.isDebug) Logger.v("--setLoginId--", "--极光Tag-->" + uuid);
        //建议添加tag标签，发送消息的之后就可以指定tag标签来发送了
        Set<String> set = new HashSet<>();
        set.add(uuid);
        JPushInterface.setTags(this, set, null);//设置标签
        JPushInterface.setAlias(this, uuid, null);
    }

    public void openLoginActivity() {
        ActivityManager manager = ActivityManager.getManager();
        Activity topActivity = manager.getTopActivity();
        if (topActivity == null) {
            LoginActivity.openNew(this);
        } else LoginActivity.open(topActivity);
    }
}
